2
2
.
.
5
5
.
.
2
2
P
P
r
r
o
o
p
p
e
e
r
r
t
t
i
i
e
e
s
s
-
-
P
P
r
r
i
i
v
v
a
a
t
t
e
e
-
-
G
G
e
e
t
t
t
t
e
e
r
r
s
s
&
&
S
S
e
e
t
t
t
t
e
e
r
r
s
s
I
I
n
n
f
f
o
o
[
[
G
G
]
]
This tutorial shows how to create Entity that has private Properties.
In order to access them we need to create additional setter and getter Methods.
You can combine Constructor and Setters where
Constructor is used to set Required or Immutable Properties
Setters are used to set Optional or Mutable Properties
Application Schema [Results]
Spring Boot Starters
GROUP
DEPENDENCY
DESCRIPTION
Web
Spring Web
Enables: @RequestMapping, Tomcat Server
PersonEntity
http://localhost:8080/Hello
Tomcat
Browser
MyController
P
P
r
r
o
o
c
c
e
e
d
d
u
u
r
r
e
e
Create Project: entity_entity (add Spring Boot Starters from the table)
Create Package: entities (inside main package)
Create Class: PersonEntity.java (inside package entities)
Create Package: controllers (inside main package)
Create Class: MyController.java (inside package controllers)
PersonEntity.java
package com.ivoronline.entity_entity.entities;
public class PersonEntity {
//PROPERTIES
private Long id;
private String name;
private Integer age;
//SETTERS
public void setId (Long id) { this.id = id; }
public void setName(String name) { this.name = name; }
public void setAge (Integer age) { this.age = age; }
//GETTERS
public Long getId () { return id; }
public String getName() { return name; }
public Integer getAge () { return age; }
}
MyController.java
package com.ivoronline.entity_entity.controllers;
import com.ivoronline.entity_entity.entities.PersonEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class MyController {
PersonEntity personEntity = new PersonEntity();
@ResponseBody
@RequestMapping("/Hello")
public String hello() {
personEntity.setName("John");
String name = personEntity.getName();
return "Hello " + name;
}
}
R
R
e
e
s
s
u
u
l
l
t
t
s
s
http://localhost:8080/Hello
Application Structure
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>